//Stephen Erb Application GUI

import javax.swing.*;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.text.AbstractDocument;
import javax.swing.text.MaskFormatter;

public class Student extends Person implements ActionListener{
        JFrame g;
        JFrame s;
        JFrame m;
        JFrame a;
	JButton okS;
	JButton closeS;
	private long sID;
	private String advisor;
	private String major;
	private double gPA;
	JTextField sIDT;
	JTextField adviT;
	JTextField majorT;
	JTextField gPAT;
	public Student() throws Exception
	{	
		sID=0;
		advisor="";
		major="";
		gPA=0;
		setLayout(new GridLayout(10, 4, 5, 5));
		JLabel sIDL= new JLabel("Student ID: ");
                sIDT= new JTextField(16);
                ((AbstractDocument) sIDT.getDocument()).setDocumentFilter(new SIDFieldDocumentFilter());
		JLabel adviL= new JLabel("Advisor: ");
                adviT= new JTextField(25);
                ((AbstractDocument) adviT.getDocument()).setDocumentFilter(new AdvisorFieldDocumentFilter());
		JLabel majorL= new JLabel("Major: ");
                majorT= new JTextField(25);
                ((AbstractDocument) majorT.getDocument()).setDocumentFilter(new AdvisorFieldDocumentFilter());
		JLabel gPAL= new JLabel("GPA: ");
                gPAT= new JTextField(4);
                ((AbstractDocument) gPAT.getDocument()).setDocumentFilter(new GPAFieldDocumentFilter());
		okS=new JButton("OK");
		closeS=new JButton("Close");
		okS.setActionCommand("OK");
		closeS.setActionCommand("CLOSE");
		okS.addActionListener(this);
		closeS.addActionListener(this);
		add(sIDL);
		add(sIDT);
		add(adviL);
		add(adviT);
		add(majorL);
		add(majorT);
		add(gPAL);
		add(gPAT);
		add(okS);
		add(closeS);
	}
        public boolean validateAdvisor() {
            boolean valid=true;
            boolean size=true;
            char[] temp=adviT.getText().trim().toCharArray();
            for(int i=0; i<temp.length;i++) {
                if(temp[i]<'A' || temp[i]>'Z')
                {
                    if(temp[i]<'a' || temp[i]>'z')
                    {
                        if(temp[i]!=' ')valid=false;
                    }
                }
            }
            if(temp.length>25 || temp.length==0)size=false;
            if(valid==false || size==false) {
                a=new JFrame("Advisor Error Message Box");
                a.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close= new JButton("Close");
                close.setActionCommand("ADVISORERRORCLOSEBOX");
                close.addActionListener(this);
                JLabel eL=new JLabel();
                if(valid==false && size==true) {
                    eL.setText("Only letters and spaces!");   
                }else if(valid==false && size==false) {
                    eL.setText("Only letters and spaces! Fewer than twenty-five.");
                }else if(valid==true && size==false) {
                    eL.setText("A maximum of twenty-five letters and spaces");
                }
                a.add(eL);
                a.add(close);
                a.setSize(400,300);
                a.setLocationRelativeTo(null);
                a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                a.setVisible(true);
            }
            if(valid&&size) {
                setAdvisor(adviT.getText().trim());
            }
            return (valid && size);   
        }
        public boolean validateMajor() {
            boolean valid=true;
            boolean size=true;
            char[] temp=majorT.getText().trim().toCharArray();
            for(int i=0; i<temp.length;i++) {
                if(temp[i]<'A' || temp[i]>'Z')
                {
                    if(temp[i]<'a' || temp[i]>'z')
                    {
                        if(temp[i]!=' ')valid=false;
                    }
                }
            }
            if(temp.length>25 || temp.length==0)size=false;
            if(valid==false || size==false) {
                m=new JFrame("Major Error Message Box");
                m.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close= new JButton("Close");
                close.setActionCommand("MAJORERRORCLOSEBOX");
                close.addActionListener(this);
                JLabel eL=new JLabel();
                if(valid==false && size==true) {
                    eL.setText("Only letters!");   
                }else if(valid==false && size==false) {
                    eL.setText("Only letters! Fewer than twenty-five.");
                }else if(valid==true && size==false) {
                    eL.setText("A maximum of twenty-five letters");
                }
                m.add(eL);
                m.add(close);
                m.setSize(400,300);
                m.setLocationRelativeTo(null);
                m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                m.setVisible(true);
            }
            if(valid&&size) {
                setMajor(majorT.getText().trim());
            }
            return (valid && size);   
        }
        
        public boolean validateGPA() {
            boolean valid=true;
            boolean size=true;
            char[] temp=gPAT.getText().toCharArray();
            for(int i=0; i<=temp.length-4;i++) {
                if(temp[i]<'0' || temp[i]>'9')valid=false;
            }
            if(temp.length>=4) {      
                if(temp[temp.length-(2+1)]!='.') {
                    valid=false;
                }else if(temp[temp.length-(2+1)]=='.') {
                    for(int i=temp.length-2; i<temp.length; i++) {
                        if(temp[i]<'0' || temp[i]>'9')valid=false;
                    }
                }
            }else valid=false;    
            if(temp.length!=4)size=false;
            if(valid==false || size==false) {
                g=new JFrame("Grade Point Average Error Message");
                g.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close= new JButton("Close");
                close.setActionCommand("GERRORCLOSEBOX");
                close.addActionListener(this);
                JLabel eL=new JLabel();
                if(valid==false && size==true) {
                    eL.setText("The format is number.numbernumber (Example: 4.00).");   
                }else if(valid==false && size==false) {
                    eL.setText("The format is number.numbernumber (Example: 4.00). Four characters.");
                }else if(valid==true && size==false) {
                    eL.setText("Four characters.");
                }
                g.add(eL);
                g.add(close);
                g.setSize(500,300);
                g.setLocationRelativeTo(null);
                g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                g.setVisible(true);
            }
            if(valid&&size) {
                setGPA(Double.parseDouble(gPAT.getText()));
            }
            return (valid && size);
        }
        public boolean validateSID() {
            boolean valid=true;
            boolean size=true;
            char[] temp=sIDT.getText().toCharArray();
            for(int i=0; i<temp.length;i++) {
                if(temp[i]<'0' || temp[i]>'9')valid=false;
            }
            if(temp.length!=16)size=false;
            if(valid==false || size==false) {
                s=new JFrame("Student ID Error Message");
                s.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close= new JButton("Close");
                close.setActionCommand("SIDERRORCLOSEBOX");
                close.addActionListener(this);
                JLabel eL=new JLabel();
                if(valid==false && size==true) {
                    eL.setText("Only numbers!");   
                }else if(valid==false && size==false) {
                    eL.setText("Only numbers! Sixteen numbers to be exact.");
                }else if(valid==true && size==false) {
                    eL.setText("Only sixteen numbers, no more no less than sixteen.");
                }
                s.add(eL);
                s.add(close);
                s.setSize(400,300);
                s.setLocationRelativeTo(null);
                s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                s.setVisible(true);
            }
            if(valid&&size) {
                setSID(Long.valueOf(sIDT.getText()).longValue());
            }
            return (valid && size);
        }
	public void initialize()
	{
		this.setTitle("Student");
		remove(ok);
		remove(close);
		this.setSize(400,300);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
        public boolean proceedOnward() 
        {
            boolean valid=true;
            if(validateGPA()==false) valid=false;
            if(validateSID()==false) valid=false;
            if(validateMajor()==false) valid=false;
            if(validateAdvisor()==false) valid=false;
            if(validateFName()==false) valid=false;
            if(validateMName()==false)valid=false;
            if(validateLName()==false) valid=false;
            if(validateAge()==false) valid=false;
            if(validateSSN()==false) valid=false;
            return valid;
        }
	public void actionPerformed(ActionEvent event)
	{
		String command=event.getActionCommand();
		if(command=="OK")
		{
                        if(proceedOnward()) {
                            System.out.println(toString());
                            System.exit(0);
                        }
		}
		else if(command=="CLOSE")
		{
			dispose();
                        Choose c1=new Choose();
                        c1.initialize();
		}
                else if(command=="GERRORCLOSEBOX") 
                {
                        g.dispose();
                }
                else if(command=="SIDERRORCLOSEBOX") 
                {
                        s.dispose();
                }
                else if(command=="MAJORERRORCLOSEBOX") 
                {
                        m.dispose();
                }
                else if(command=="ADVISORERRORCLOSEBOX") 
                {
                        a.dispose();
                }
	}
	public long getSID()
	{
		return sID;
	}
	public String getAdvisor()
	{
		return advisor;
	}
	public String getMajor()
	{
		return major;
	}
	public double getGPA()
	{
		return gPA;
	}
        public void setSID(long temp) 
        {
                sID=temp;
        }
        public void setAdvisor(String temp) 
        {
                advisor=temp;
        }
        public void setMajor(String temp) 
        {
                major=temp;
        }
        public void setGPA(double temp) 
        {
                gPA=temp;
        }
	public String toString()
	{
		return "\nFirst Name: " + getFName() + "\nLast Name: " + getLName() + "\nMiddle Name: " + getMName() + "\nAge:" + getAge() + "\nSSN: " + stringSSN() +"\nSID: "+ getSID()+ "\nAdvisor: " + getAdvisor()+ "\nMajor: "+getMajor()+ "\nGPA: "+ getGPA();
	}
}